<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Starter Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <style>
        body {
            padding-top: 50px;
        }

        .starter-template {
            padding: 40px 15px;
            text-align: center;
        }
    </style>

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .panel-body{height: 300px;overflow-y: auto}
        #msgBox p{background: #4ab54a;padding: 10px;border-radius: 6px;color: #fff;max-width: 50%;width: fit-content;clear: both}
        #msgBox .send{background: #b5854a;float: right}
    </style>
</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#about">About</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</div>

<div class="container">
    <div class="panel panel-default" style="margin-top: 20px">
        <div class="panel-heading">
            <h3 class="panel-title">聊天室测试 <span class="pull-right text-success" style="font-size: 12px"><span id="loginInfo"></span></span></h3>
        </div>
        <div class="panel-body" id="msgBox">
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="form-group">
                <div class="col-sm-12">
                    <textarea class="form-control" id="content" rows="10" placeholder="输入要发送的内容"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <input type="button" id="send" class="btn btn-success pull-right"  value="确认发送" style="margin-top: 10px">
                </div>
            </div>
        </div>
    </div>
</div><!-- /.container -->

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    var username = "李四";
    ws = new WebSocket("ws://127.0.0.1:8282");
    var data = '{"type":"init","id":2,"avatar":"none","sign":"签名","username":"'+username+'"}';
    ws.onopen = function() {
        console.log('连接成功，登陆中……')
        ws.send(data);
        console.log("登陆成功");
    };
    ws.onmessage = function(e) {
        var data =  JSON.parse(e.data);
        if (data.type==='login'){
            $("#loginInfo").text(data.username+"上线了~~");
        }
        if(data.type==='chatMessage'){
            var content = data.data.content;
            var username =  data.data.username;
            regHtml(username,content);
        }
        console.log(data)
    };
    $("#send").click(function () {
        var content = $("#content").val()
        send(content);
    });
    function send(content) {
        var data = '{"type":"chatMessage","data":{"to":{"type":"uid","id":"1"},"mine":{"content":"'+content+'"}}}';
        $("#content").val("");
        regHtml(username,content,'send');
        ws.send(data);
    }
    function regHtml(username,content,className) {
        var html = $("#msgBox").html();
        html+="<p class='"+className+"'>"+content+"</p>";
        $("#msgBox").html(html);
    }
</script>
</body>
</html>